package com.base.mail;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;

import com.base.commonlib.bean.Partner;
import com.base.commonlib.event.PartenrEvent;
import com.base.commonlib.utils.RxBus2;
import com.base.mail.adapter.AJParterAdapter;
import com.base.mail.databinding.ActivityAjpartenerBinding;
import com.base.mail.presenter.AjPartenerPrestener;
import com.base.mail.tree.Node;
import com.base.mail.view.AjPartenerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 安监搭档选择界面
 * Created by weikailiang on 2019/11/13.
 */
@Route(path = "/mail/ajpartener")
public class AjPartenerActivity extends CBaseBarMvpActivity<AjPartenerPrestener> implements AjPartenerView {
    private ActivityAjpartenerBinding mBinding;
    private List<Partner> mPartener = new ArrayList<>();
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_ajpartener;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityAjpartenerBinding) mRootBinding;
        mPresenter = new AjPartenerPrestener(this,this);
        setBackNavigation();
        setBarTitle("搭档选择");
    }

    @Override
    protected void start() {
        mPresenter.start();
        mPresenter.getParenter();
        mBinding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPartener.clear();
                Partner partner;
                for (Map.Entry<String, Node> node : AJParterAdapter.mMapPart.entrySet()){
                    partner = new Partner();
                    partner.setCompanyId(node.getValue().getpId().toString());
                    partner.setParentId(mPresenter.mUser.getUserId());
                    partner.setId(node.getValue().getId().toString());
                    partner.setUserName(node.getValue().getName());
                    mPartener.add(partner);
                }
                BaseApp.setPatters(mPartener,mPresenter.mUser);
                RxBus2.getInstance().post(new PartenrEvent());
                finish();
            }
        });
    }

    @Override
    public RecyclerView getRecycler() {
        return mBinding.rv;
    }

    @Override
    public void returnPartner(String name) {

        mBinding.name.setText(name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
        if (AJParterAdapter.mMapPart.size()>0){
            AJParterAdapter.mMapPart.clear();
        }
    }
}
